import UIKit



let string = "Hello, world!"

// 获取某个位置的索引
let index = string.index(string.startIndex, offsetBy: 7)
print("String.Index at position 7: \(index)")

// 将 String.Index 转换为 Int
let distanceFromStart = string.distance(from: string.startIndex, to: index)
print("Distance from start to index: \(distanceFromStart)")

// 使用 Int 值获取对应的 String.Index
if let newIndex = string.index(string.startIndex, offsetBy: distanceFromStart, limitedBy: string.endIndex) {
  print("New String.Index: \(newIndex)")
}






let text = "Swift is awesome!"
let startIndex = text.startIndex
let endIndex = text.index(startIndex, offsetBy: 10)

// 计算 startIndex 到 endIndex 的距离
let distance = text.distance(from: startIndex, to: endIndex)
print("Distance: \(distance)") // 输出: 10

// 使用距离重新计算索引
let newIndex = text.index(startIndex, offsetBy: distance)
print("New index: \(newIndex)") // 输出: 10

text[newIndex]

